package com.example.viewpagerdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.example.viewpagerdemo.fragment.AnimeFragment;
import com.example.viewpagerdemo.fragment.KingGloryFragment;
import com.example.viewpagerdemo.fragment.LoLFragment;
import com.example.viewpagerdemo.fragment.TvFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    public ViewPager viewPager;
    public TabLayout tabLayout;

    List<View> viewList = new ArrayList<>();

    ArrayList<String> titleList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();


    }

    private void initData() {

        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new AnimeFragment());
        fragmentList.add(new TvFragment());
        fragmentList.add(new KingGloryFragment());
        fragmentList.add(new LoLFragment());

        String[] arr = new String[]{
                "动漫", "电视剧", "王者荣耀", "LOL"
        };
        titleList.addAll(Arrays.asList(arr));

        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,
                fragmentList,
                titleList));
        tabLayout.setupWithViewPager(viewPager,false);
    }

    private void initView() {
        viewPager = findViewById(R.id.viewpager);
        tabLayout = findViewById(R.id.my_tab_layout);
    }
}